package com.example.demo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bean.currency.Sucess;
import com.example.demo.bean.user.User;
import com.example.demo.bean.currency.UserSelect;
import com.example.demo.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api")
@RestController
@SpringBootApplication
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    //    新增员工
    @PostMapping("/addUser")
    public Sucess addUser(@RequestBody User user) {
        Sucess sucess = new Sucess();
        if (userMapper.selectById(user.getId()) == null) {
            userMapper.insert(user);
        } else {
            userMapper.updateById(user);
        }
        return sucess;

    }

    @PostMapping("/getUserList")
    public IPage<User> getUserList(@RequestBody UserSelect<User> select) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(select.getQueryObj() !=null) {
            if(select.getQueryObj().getName() !=null) {
                wrapper.like("name",select.getQueryObj().getName());
            }
            if(select.getQueryObj().getAge() != null) {
                wrapper.like("age", select.getQueryObj().getAge());
            }
            if(select.getQueryObj().getEmail() != null) {
                wrapper.like("email", select.getQueryObj().getEmail());
            }
            if(select.getQueryObj().getDepId() != null) {
                wrapper.like("dep_id", select.getQueryObj().getDepId());
            }
        }
        wrapper.inSql("dep_id", "SELECT id FROM department WHERE name LIKE '%" + select.getQueryObj().getDepIdName()  + "%'");

        Page<User> page = new Page<User>(select.getPageNumber(), select.getPageSize(),1);
        return userMapper.selectPage(page, wrapper);

    }
}
